<?php


namespace app\model;

use think\Model;
use think\model\concern\Virtual;

class Panel extends Model {
    use Virtual;
    var $_config;
    var $_panel_type;
    public function __construct(array $data = [])
    {
        parent::__construct($data);
        $this->_config=config('capture.panel');
        $this->_panel_type=['1'=>'HIK','2'=>'HIKCom','99'=>'ZZ'];
    }


    public function add_people($aizone_id,$id,$name,$card_num,$photo){
        $aizone=Aizone::where('name_code',$aizone_id)->find();
        if($aizone){
            $lists=ViidDevice::where('aizone_id',$aizone['id'])->group('op_rights')->select();
            foreach ($lists as $list){
                $type=$this->_panel_type[$list['panel_type']];
                $service=null;
                eval('$service= new app\service\impl\Panel'.$type.'ServiceImpl();');
                $result=$service->add_people($id,$name,$card_num,$photo,$list['op_rights']);
            }
            return $result;
        }
    }

    public function bind_group($op_rights, $ape_id,$panel_type) {
        $type=$this->_panel_type[$panel_type];
        $service=null;
        eval('$service= new app\service\impl\Panel'.$type.'ServiceImpl();');
        $op_groups=$service->bind_group($op_rights,$ape_id);
    }

    private function getToken() {
        return [CURLOPT_HTTPHEADER=>['Content-Type:application/json','Authorization:bearer '.$this->_config['access_token']]];
    }

    public function add_group($id,$panel_type,$aizone_name,$ape_id,$third_code){
        $type=$this->_panel_type[$panel_type];
        $service=null;
        eval('$service= new app\service\impl\Panel'.$type.'ServiceImpl();');
        $op_groups=$service->add_group($aizone_name.'权限组',$ape_id,$third_code);
        if($op_groups){
            ViidDevice::where('id',$id)->save(['op_rights'=>$op_groups]);
        }
    }

}